package com.fudan.biz.data.web;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.biz.data.entity.BigDataSource;
import com.fudan.biz.data.service.BigDataSourceService;
import com.fudan.biz.data.service.FieldMetaService;
import com.fudan.es.entity.FieldMeta;
import com.fudan.framework.base.DaoService;
import com.fudan.framework.base.DataController;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.entity.TableJson;

@Controller
@RequestMapping("/biz/data/ds")
public class BigDataSourceController extends DataController<BigDataSource>{

	@Autowired
	private BigDataSourceService bigDataSourceService;

	@Autowired
	private FieldMetaService fieldMetaService;

	@Override
	protected DaoService<BigDataSource> injectService() {
		return bigDataSourceService;
	}

	@RequestMapping("/ds_list")
	@ResponseBody
	public TableJson getDatasourceTable(HttpServletRequest request) {
		return TableJson.build(bigDataSourceService.getInfoList(buildPage(request)), bigDataSourceService.countInfoList());
	}

	@RequestMapping("/field_list")
	@ResponseBody
	public TableJson getFields(HttpServletRequest request) {
		String id = request.getParameter("id");
		String searchable = request.getParameter("searchable");
		String aggregatable = request.getParameter("aggregatable");
		if(id==null || id.equals("")) {
			return TableJson.build(FIND_ERROR_CODE, FIND_ERROR_MSG);
		}else {
			FieldMeta fieldMeta = new FieldMeta();
			fieldMeta.setDsId(Integer.parseInt(id));
			System.out.println("---->"+id);
			String search = request.getParameter("search_value");
			fieldMeta.setName(search);
			if(searchable !=null) {
				fieldMeta.setSearchable(Boolean.valueOf(searchable));
			}
			if(aggregatable !=null) {
				fieldMeta.setAggregatable(Boolean.valueOf(aggregatable));
			}
			return TableJson.build(fieldMetaService.getFieldByQuery(fieldMeta, buildPage(request)), fieldMetaService.countFieldByQuery(fieldMeta));
		}
	}
	
	@RequestMapping("/fields")
	@ResponseBody
	public ResponseMsg getFieldList(HttpServletRequest request) {
		String id = request.getParameter("id");
		String key = request.getParameter("key");
		List<FieldMeta> fields = fieldMetaService.getFieldByDsId(id,key);
		List<Map<String,Object>> selector = new LinkedList<>();
		for (FieldMeta fieldMeta : fields) {
			Map<String,Object> param = new HashMap<>();
			param.put("name", fieldMeta.getAlias());
			param.put("value", fieldMeta.getId());
			selector.add(param);
		}
		return ResponseMsg.build().putData(selector);
	}

	@RequestMapping("/field/update_alias")
	@ResponseBody
	public ResponseMsg updateFieldAlias(HttpServletRequest request) {
		String alias = request.getParameter("alias");
		String id = request.getParameter("id");
		if(alias == null || id == null || alias.equals("")||id.equals("")) {
			return ResponseMsg.build(UPDATE_ERROR_CODE, "更新失败");
		}else {
			fieldMetaService.updateAlias(id, alias);
			return ResponseMsg.build(SUCCESS_CODE,SUCCESS);
		}
	}
	@RequestMapping("/field/get")
	@ResponseBody
	public ResponseMsg getField(HttpServletRequest request) {
		try {
			String id = request.getParameter("id");
			FieldMeta field = fieldMetaService.get(id);
			return ResponseMsg.build().putData(field);
		}catch(Exception e) {
			return ResponseMsg.build(SYSTEM_ERROR_CODE);
		}
	}


	@RequestMapping("/query_ds_list")
	@ResponseBody
	public TableJson getDsTable(HttpServletRequest request) {
		String query = fetchSearchVal(request);
		if(query == null || query.equals("")) {
			return getDatasourceTable(request);
		}else {
			return TableJson.build(bigDataSourceService.getInfoList(query,buildPage(request)), bigDataSourceService.countInfoList(query));
		}
	}

	@RequestMapping("/info")
	@ResponseBody
	public ResponseMsg info(HttpServletRequest request) {
		String id = request.getParameter("id");
		if(id == null || id.equals("")) {
			return ResponseMsg.build(FIND_ERROR_CODE, FIND_ERROR_MSG);
		}
		return ResponseMsg.build().putData(bigDataSourceService.getInfo(id));
	}

	@Override
	public Boolean delete(HttpServletRequest request) {
		return bigDataSourceService.delete(request.getParameter("id"));
	}


	@Override
	protected String pathPrefix() {
		return "biz/datasource";
	}


}
